<?php
namespace App\Logic\calculate;

use Illuminate\Support\Facades\Cache;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\DB;

class EthMining
{
    public static function index()
    {
        //挖自己的，发大家的
        /**
         * 获取单T应发
         *
         *  获取每个人的容量
         *  发放单个人的到矿机收益记录里面
         *
         *  获取总容量
         *  获取每个等级人数
         *  按奖励给每个等级的人奖励
         */
        $eth_config = Cache::get('configs')['eth'];

        //初始
        $per_fil = 100;
        $per_eth = $eth_config['day_mining_num']->val;


        //个人单T应发
        $fil_per = $per_fil * 0.8 * 0.7;
        $eth_per = $per_eth * 0.7;

        $d =  $eth_config['start_day']->val;
        $d = $d - 1;
        $time = time();
        $start_time = $time - 86400 * $d;

        $start_date = date('Y-m-d',$start_time);


        $configs = Cache::get('configs');

        if(isset($configs['fee']['miner_fee']->val)){
            $miner_fee = $configs['fee']['miner_fee']->val;
        }else{
            $miner_fee = 0;
        }


        $list = DB::table('miner_host as mh')
            ->leftJoin('miner_info as mi','mi.mid','=','mh.mid')
            ->leftJoin('miner_user as mu','mu.mid','=','mh.mid')
            ->leftJoin('user as u','u.username','=','mu.user_name')
            ->where('mh.token','FIL')
            ->where('mh.add_time','<',$start_date)
            ->where('mh.end_time','>',$time)
            ->select('mh.id','mh.mid','mh.add_time','mh.start_time','mh.hspace','mh.note','mu.user_name','u.id as uid','u.level','u.rebate','u.username','u.phone')
            ->get()->toArray();

        if(!$list){
            return e_msg(400001,'数据为空');
        }



        $hspace_arr = array_column($list,'hspace');
        $eth_sum = array_sum($hspace_arr);
        $fil_sum = DB::table('miner_host')
            ->where('start_time','<',$time)
            ->where('end_time','>',$time)
            ->sum('hspace');
        $fil_base = $per_fil * 0.8 *  $fil_sum;
        $eth_base = $per_eth  *  $eth_sum;


        $eth_insert_arr = [];
        $fil_insert_arr = [];
        $src_insert_arr = [];
        $fil_user_insert_arr = [];
        $eth_user_insert_arr = [];


        //note    1 不能正常发   0 正常发

        $src_user_name_arr = [];

        if($miner_fee == 0){
            foreach($list as $k => $v) {

                $eth = $eth_per * $v->hspace;
                $fil = $fil_per * $v->hspace;

                $eth_insert_arr[] = [
                    'token' => 'ETH',
                    'mid' => $v->mid,
                    'amount' => $eth,
                    'ticket' => 'data_ETH_Mining_' . date('YmdHis'),
                    'type' => 'mining',
                    'type_note' => '挖矿',
                    'state' => 1,
                ];
                if($v->start_time > $time) {
                    $fil_insert_arr[] = [
                        'token' => 'FIL',
                        'mid' => $v->mid,
                        'amount' => $fil,
                        'ticket' => 'data_Mining_' . date('YmdHis'),
                        'type' => 'mining',
                        'type_note' => '挖矿',
                        'state' => 1,
                    ];
                }
                if($v->note == 0){
                    DB::table('miner_host')->where('id',$v->id)->update(['note'=>1]);
                }

            }

        }else {
            foreach($list as $k => $v) {

                $eth = $eth_per * $v->hspace;
                $fil = $fil_per * $v->hspace;
                $srt = $miner_fee * $v->hspace;

                if($srt <= $v->rebate ){

                    $eth_insert_arr[] = [
                        'token' => 'ETH',
                        'mid' => $v->mid,
                        'amount' => $eth,
                        'ticket' => 'data_Mining_' . date('YmdHis'),
                        'type' => 'mining',
                        'type_note' => '挖矿',
                        'state' => 1,
                    ];

                    if($v->start_time > $time) {
                        $fil_insert_arr[] = [
                            'token' => 'FIL',
                            'mid' => $v->mid,
                            'amount' => $fil,
                            'ticket' => 'data_Mining_' . date('YmdHis'),
                            'type' => 'mining',
                            'type_note' => '挖矿',
                            'state' => 1,
                        ];
                    }

                    $src_insert_arr[] = [
                        'out_account' => $v->username,
                        'out_phone' => $v->phone,
                        'shift_account' => '系统',
                        'shift_phone' => '系统',
                        'amount' => -$srt,
                        'type_note' => '算力托管费',
                        'type' => 'consumption_fee',
                    ];

                    DB::table('user')->where('username',$v->username)->decrement('rebate',$srt);
                    if($v->note == 1){
                        DB::table('miner_host')->where('id',$v->id)->update(['note'=>0]);
                    }

                }else{

                    if($v->note == 0){
                        DB::table('miner_host')->where('id',$v->id)->update(['note'=>1]);
                    }
                }
            }
        }


        if ( count($eth_insert_arr) > 0){
            Db::table('miner_balance')->insert($eth_insert_arr);
        }

        if ( count($fil_insert_arr) > 0){
            Db::table('miner_balance')->insert($fil_insert_arr);
        }


        if ( count($src_insert_arr) > 0 ){
            DB::table('record_srt')->insert($src_insert_arr);
        }

        $user_count = Db::table('user')
            ->where('state','1')
            ->where('level','>',0)
            ->select('level',DB::raw('count(*) as count'))
            ->groupBy('level')
            ->get()->toArray();

        foreach($user_count as $user_count_val) {

            $user = Db::table('user')
                ->where('state','1')
                ->where('level',$user_count_val->level)
                ->select('username')
                ->get()->toArray();
            $calculate = self::get_calculate($user_count_val->level);

            $fil_amount = $fil_base * $calculate /100/$user_count_val->count;
            $eth_amount = $eth_base * $calculate /100/$user_count_val->count;
            foreach($user as $user_val){
                if($fil_amount > 0){
                    $fil_user_insert_arr[] = [
                        'token' => 'FIL',
                        'username' => $user_val->username,
                        'amount' => $fil_amount,
                        'ticket' => 'data_Red_' . date('YmdHis'),
                        'type' => 'profit',
                        'type_note' => '分红',
                        'state' => 1,
                    ];
                }
                if($eth_amount){
                    $eth_user_insert_arr[] = [
                        'token' => 'ETH',
                        'username' => $user_val->username,
                        'amount' => $eth_amount,
                        'ticket' => 'data_Red_' . date('YmdHis'),
                        'type' => 'profit',
                        'type_note' => '分红',
                        'state' => 1,
                    ];
                }
            }
        }

        if( count($fil_user_insert_arr) > 0 ){
            Db::table('user_balance')->insert($fil_user_insert_arr);
        }
        if( count($eth_user_insert_arr) > 0 ){
            Db::table('user_balance')->insert($eth_user_insert_arr);
        }

    }

    public static function get_calculate($level){

        $config_level = Cache::get('config_level');

        $level_arr = array_column($config_level,'level');
        if(in_array($level,$level_arr)){
            $key = array_keys($level_arr,$level)[0];
        }else{
            array_push($level_arr,$level);
            sort($level_arr);
            $key = array_keys($level_arr,$level)[0]-1;
        }
        $calculate = $config_level[$key]->calculate;
        return $calculate;
    }
}





